package com.king.base;

import java.util.Map;
import java.util.regex.Pattern;

import com.google.common.base.Splitter;
import com.king.print.Printer;

/**
 * @author taomk
 * @version 1.0
 * @since 15-1-25 下午10:21
 */
public class SplitterTest {
	public static void main(String [] args) {

		// Guava中Splitter的用法
		String numbersAsString = "1;2;3;4;5";
		Iterable<String> numbers = Splitter.on(";").split(numbersAsString);
		numbersAsString = "foo , what,,,more,"; // 对于这样的字符串进行切分
		numbers = Splitter.on(",").omitEmptyStrings().trimResults().split(numbersAsString);

		String testStr = " foo, ,bar, quux,";

		Iterable<String> iter1 = Splitter.on(",").split(testStr);
		Printer.print(iter1);

		// Splitter is configurable, such as follows
		Iterable<String> iter2 = Splitter.on(",").trimResults().omitEmptyStrings().split(testStr);
		Printer.print(iter2);

		// Splitter can split string to a map
		testStr = "1=a&2=b&3=c";
		Map<String, String> result = Splitter.on("&").withKeyValueSeparator("=").split(testStr);
		Printer.print(result);

		// Splitter can also split with a fixed length
		testStr = "12345678abcdef";
		Iterable<String> iter3 = Splitter.fixedLength(3).split(testStr);
		Printer.print(iter3);

		// Splitter can also split by regular expression
		testStr = "Xabc1231324234Ycde1234567890Zxyz0987654321";
		Iterable<String> iter4 = Splitter.onPattern("[a-z]{3}\\d{10}").split(testStr);
		Printer.print(iter4);

		Iterable<String> iter5 = Splitter.on(Pattern.compile("[a-z]{3}\\d{10}")).split(testStr);
		Printer.print(iter5);

		// Splitter can also limit split times
		Iterable<String> iter6 = Splitter.on(Pattern.compile("[a-z]{3}\\d{10}")).limit(2).split(testStr);
		Printer.print(iter6);
	}
}
